﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using BrainTechLLC.ThreadSafeObjects;
using LinFu.AOP.Interfaces;

namespace BrainTechLLC
{
	[Serializable, AttributeUsage(AttributeTargets.Method)]
	public class ObservableMethodAttribute : GenericAttributeBool, IReadableAttribute
	{
		public bool IsObservable { get { return Val; } }

		public ObservableMethodAttribute(bool isObservable) : base(isObservable) { }
		public ObservableMethodAttribute() : base(true) { }

		public void DoWorkBefore(IInvocationContext context, object thisObject)
		{
			Type t = thisObject.GetType();
			Observable observable = Lookups.ObservableMethods.Get(t.DotMethodName(context.TargetMethod.Name));
			observable.RaiseNotify(true, thisObject, context, null, null);
		}

		public void DoWorkAfter(IInvocationContext context, object thisObject, object returnValue)
		{
			Type t = thisObject.GetType();
			Observable observable = Lookups.ObservableMethods.Get(t.DotMethodName(context.TargetMethod.Name));
			observable.RaiseNotify(false, thisObject, context, returnValue, null);
		}
	}
}
